/**
 * 
 */
package co.fitstart.mobile.web.controller.trading;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.core.BusinessException;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.product.Product;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.agent.AgentDiscount;
import co.fitstart.mobile.entity.trading.CouponMapper;
import co.fitstart.mobile.service.CouponService;
import co.fitstart.mobile.service.ProductService;
import co.fitstart.mobile.service.UserAgentService;
import co.fitstart.mobile.service.UserService;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * Trading assist related controller, including 'user binding', 'coupons'
 * 
 * @author downpour
 *
 */
@Controller
@RequestMapping("")
public class TradingAssistController {
    
    private static final Logger logger = LoggerFactory.getLogger(TradingAssistController.class);
    
    @Autowired
    private CouponService couponService;
    
    @Autowired
    private ProductService productService;
    
    @Autowired
    private UserAgentService userAgentService;
    
    @Autowired
    private UserService userService;
        
    /**
     * List all user's available coupons
     * 
     * @param user
     * @param productId
     * @return
     */
    @RequestMapping(value = "/shop/coupons", method = RequestMethod.GET, params = "sku")
    public @ResponseBody String coupons(@Secure() User user,
                                        @RequestParam("sku") Long productId) {
        
        Result result = new Result();
        
        try {
            
            Product product = productService.load(Product.class, productId);
            
            List<Coupon> coupons = couponService.listCoupon(user, product);
            
            List<CouponMapper> couponMappers = new ArrayList<CouponMapper>();
            
            for(Coupon coupon : coupons) {
                couponMappers.add(new CouponMapper(coupon, product));
            }
            
            result.success()
                  .data("coupons", couponMappers.isEmpty() ? null : couponMappers)
                  .message("获取优惠券成功！");
            
        } catch (Exception e) {
            
            logger.info("TradingAssistController#coupons()", e);
            
            result.fail()
                  .message("获取优惠券失败！");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * List discount by code
     * 
     * @param user
     * @param productId
     * @return
     */
    @RequestMapping(value = "/shop/discount-code", method = RequestMethod.GET)
    public @ResponseBody String discountCode(@Secure() User user,
                                             @RequestParam("code") String code,
                                             @RequestParam("price") int price,
                                             @RequestParam("type") String type) {
        
        Result result = new Result();
        
        try {
            
            if(StringUtils.equals(type, "online")) {
                AgentDiscount agentDiscount = userAgentService.loadAgentDiscount(code);
                result.data(agentDiscount.describe(price));
            } else if (StringUtils.equals(type, "offline")) {
                Student student = userService.getStudent(code);
                result.data(student.describe());
            }
            
            result.success()
                  .message("获取折扣码成功！");
            
        } catch (BusinessException e) {
            
            result.fail().message(e.getMessage());
            
        } catch (Exception e) {
            
            logger.info("TradingAssistController#discountCode()", e);
            
            result.fail()
                  .message("获取折扣码失败！");
        }
        
        return JsonUtils.toJsonString(result);
    }
}
